package com.victualler.core.config.dict.service.impl;

import com.victualler.core.base.service.impl.BaseServiceImpl;
import com.victualler.core.common.dto.list.DictComDto;
import com.victualler.core.config.dict.entity.DictDao;
import com.victualler.core.config.dict.dto.*;
import com.victualler.core.config.dict.repository.DictQueryRepository;
import com.victualler.core.config.dict.repository.DictRepository;
import com.victualler.core.config.dict.service.DictService;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: Guoshichang
 * @Date: Create in 17:03 2018/4/5
 * @Discription:  Dict字典类型对应的服务层
 */
@Service
public class DictServiceImpl extends BaseServiceImpl<DictDao,DictRepository,DictQueryRepository> implements DictService {

    @Override
    public List<DictTopDto> getDictTopDtoList() {
        return this.baseRepository.findDictTopList();
    }

    /**
     * @author Guoshichang
     * @since 20:26 on 2018/8/27.
     * @param codeType 字典类型
     * @return List
     * @Function 根据codeType 模糊查询
     */
    public List<DictTopDto> getDictTopDtoList(String codeType) {
        return this.baseQueryRepository.getDictTopDtoList(codeType);
    }

    @Override
    public List<DictDao> getDictByCodeType(String codeType) {
        return this.baseRepository.findByCodeType(codeType);
    }

    /**
     * @author Guoshichang
     * @since 13:57 on 2018/11/14.
     * @param codeType codeType
     * @param codeCode codeCode
     * @return DictDao
     * @Function  查询字典对象
     */
    public DictDao  getDictByCodeType(String codeType,String codeCode) {
        return this.baseRepository.findByCodeTypeAndCodeCode(codeType,codeCode);
    }
    /**
     * @author Guoshichang
     * @since 16:24 on 2018/8/15.
     * @param codeTypes 字典码序列
     * @return Map
     * @Function 根据字典码序列转换为map大对象 便于渲染页面
     */
    public Map<String,DictComDto> getDictComByCodeType(String[] codeTypes){
        Map<String,DictComDto> dictComDtoMap = new HashMap<>();
        for (String codeType : codeTypes) {
            List<DictDao>  list =  this.getDictByCodeType(codeType);
            Map<String,String> map = new HashMap<>();
            for (DictDao dictDao : list) {
                //将该类型的字典码放入map中 便于翻译
                map.put(dictDao.getCodeCode(),dictDao.getCodeValue());
            }
            dictComDtoMap.put(codeType,new DictComDto(list,map));
        }
        return dictComDtoMap;
    }

}
